package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.ReservaDao;
import edu.upc.clase.demo.entity.Reserva;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author gian
 */
@Repository
public class ReservaDaoImpl extends SimpleJdbcDaoSupport implements ReservaDao {

    private static Logger log = LoggerFactory.getLogger(ReservaDaoImpl.class);

    @Autowired
    public ReservaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Reserva reserva) {

        getJdbcTemplate().update(
                "insert into reserva (id_cliente, desde, hasta, costo_reserva, id_sala) values (?, ?, ?, ?, ?)",
                 reserva.getIdCliente(), reserva.getDesde(),reserva.getHasta(), reserva.getCostoReserva(), reserva.getIdSala());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }

    @Override
    public void actualizar(Reserva reserva) {
        getJdbcTemplate().update(
                "update reserva set desde = ?, hasta = ?, costo_reserva = ?, id_sala = ? where id_reserva = ? ",
                reserva.getDesde() , reserva.getHasta() , reserva.getCostoReserva(), reserva.getIdSala(), reserva.getIdReserva());
    }

    @Override
    public void eliminar(Reserva reserva) {
        getJdbcTemplate().update(
                "delete from reserva where id_Reserva = ?", reserva.getIdReserva());
    }

    @Override
    public Reserva buscar(Integer idReserva) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select id_Reserva, desde, hasta, costo_reserva from reserva where id_Reserva = ?",
                    new BeanPropertyRowMapper<Reserva>(Reserva.class), idReserva);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Reserva> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "select id_Reserva, reserva, desde, hasta from reserva",
                new BeanPropertyRowMapper<Reserva>(Reserva.class));
    }

//    @Override
//    public Sala buscar(String sala) {
//        try {
//            return getSimpleJdbcTemplate().queryForObject(
//                    "select idsala, sala from sala where sala=?",
//                    new BeanPropertyRowMapper<Sala>(Sala.class), sala);
//        } catch (EmptyResultDataAccessException e) {
//            return null;
//        }
//    }

//    @Override
//    public List<Sala> buscarPorNombre(String sala) {
//        try {
//            Map<String,String> parametros = new HashMap<String,String>();
//            parametros.put("sala","%"+sala+"%");
//            return getSimpleJdbcTemplate().query(
//                    "select * from usuarios where nombre like :sala",
//                    new BeanPropertyRowMapper<Sala>(Sala.class),parametros);
//        } catch (EmptyResultDataAccessException e) {
//            return null;
//        }
//    }

    @Override
    public Reserva buscar(String Reserva) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Reserva> buscarPorNombre(String reserva) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

//    @Override
//    public List<Reserva> buscarPorNombre(String reserva) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
}